package com.zh.config.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.ViewGroup
import androidx.core.view.isVisible
import com.blankj.utilcode.util.ScreenUtils
import com.zh.common.R
import com.zh.common.databinding.DialogTipsBinding

class TipsDialog(
    context: Context,
    private val title: String = "",
    private val content: String = "",
    private val sureTv: String = "确定",
    private val cancelTv: String = "取消",
    private val call: () -> Unit
) : Dialog(context, R.style.CustomDialog) {

    private val bing by lazy { DialogTipsBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(bing.root)
        bing.apply {
            if (title == "") {
                tvTitle.isVisible = false
            } else {
                tvTitle.isVisible = true
                tvTitle.text = title
            }
            tvCancel.setOnClickListener { dismiss() }
            tvSave.text = sureTv
            tvCancel.text = cancelTv
            tvContent.text = content
            tvSave.setOnClickListener { call.invoke();dismiss() }
        }
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        window?.apply {
            setLayout(
                (ScreenUtils.getScreenWidth() * 0.8).toInt(),
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
        }
    }
}